package com.guangyuan.network.mvc.handler;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guangyuan.network.constants.ProductGroupConstant;
import com.guangyuan.network.dataobject.ProductAttr;
import com.guangyuan.network.dataobject.ProductGroup;
import com.guangyuan.network.external.OperatorFactory;
import com.guangyuan.network.mvc.view.ContextAndTemplate;

public class addProductGroupAttrHandler extends AbstractHandler {

	private static final String NAME = "addProductGroupAttr.vm";
	
	@Override
	public ContextAndTemplate handler(HttpServletRequest request,
			HttpServletResponse response, ServletContext servletContext) {
		String group_id = request.getParameter(ProductGroupConstant.GROUP_ID);
		if (null == group_id || "".equals(group_id)) {
			return null;
		}
		int groupId = Integer.parseInt(group_id);
		
		List<ProductAttr> groupAttrs = OperatorFactory.getProductAttrOper()
				.listProductAttrByProductGroupId(groupId);
		
		ProductGroup pG = OperatorFactory.getProductGroupOper().getProductGroupById(groupId);
		
		ContextAndTemplate contextAndTemplate = getContextAndTemplate(NAME, request, response, servletContext);
		
		if(null != contextAndTemplate) {
			contextAndTemplate.getContext().put(ProductGroupConstant.GROUP_ATTRS, groupAttrs);
			contextAndTemplate.getContext().put(ProductGroupConstant.PRODUCT_GROUP, pG);
			return contextAndTemplate;
		}
		
		return null;
	}

}
